package org.example.pims.security;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.apache.poi.ss.formula.functions.T;
import org.example.commons.entity.SysAdmin;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Collection;
import java.util.List;

/**
 * 登录用户身份权限
 */
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUser implements UserDetails {

    private static final long serialVersionUID = -8706131562760799495L;

    /**
     * 电话 相当于username
     */
    private String phone;

    @JsonIgnore
    private String password;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登陆时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 角色
     */
    private List<? extends GrantedAuthority> grantedAuthorities;

    /**
     * 权限列表
     */
    private String permissions;


    /**
     * 用户信息
     */
    private Object user;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public LoginUser() {
    }


    public LoginUser(String phone, String password, List<? extends GrantedAuthority> authorities, String permissions, Object user) {
        this.phone = phone;
        this.password = password;
        this.grantedAuthorities = authorities;
        this.permissions = permissions;
        this.user = user;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return new BCryptPasswordEncoder().encode(this.password);
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return this.phone;
    }

    /**
     * 账户是否未过期,过期无法验证
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁,锁定的用户无法进行身份验证
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据(密码),过期的凭据防止认证
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用 ,禁用的用户不能身份验证
     *
     * @return
     */
    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }



    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return grantedAuthorities;
    }
}
